# -*- coding: utf-8 -*-
# File : sql_init.py
# Author: taoyahui
# Date : 2022/5/30

import sqlite3
conn = sqlite3.connect('blog.db')
cursor = conn.cursor()
try:
    cursor.execute('DROP TABLE IF EXISTS blogs')
    cursor.execute("CREATE TABLE blogs(id INTEGER PRIMARY KEY AUTOINCREMENT, title VARCHAR(40), content TEXT, create_time datetime)")
    print ("Database and table are created successfully.")
    cursor.execute("insert into blogs values(null, '第一个博客', '博客内容', '2018-08-06')")
    cursor.execute("insert into blogs values(null, '第二个博客', '博客内容', '2018-08-06')")
    cursor.execute("insert into blogs values(null, '第三个博客', '博客内容', '2018-08-06')")
    cursor.execute("insert into blogs values(null, '第四个博客', '博客内容', '2018-08-06')")
    cursor.execute('DROP TABLE IF EXISTS user')
    cursor.execute("CREATE TABLE user(username VARCHAR(20) PRIMARY KEY, password VARCHAR(20))")
    cursor.execute("INSERT INTO user VALUES('admin', '123456')")
    conn.commit()
except BaseException as e:
    print ("Error: %s" % e)